<?php

/**
 * ECSHOP 专题前台
 * ============================================================================
 * 版权所有 2005-2011 上海商派网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * @author:     webboy <laupeng@163.com>
 * @version:    v2.1
 * ---------------------------------------------
 */

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
require(dirname(__FILE__) . '/includes/lib_order.php');

if ((DEBUG_MODE & 2) != 2)
{
    $smarty->caching = true;
}

clear_cache_files();
date_default_timezone_set('PRC');

$type_id = isset($_REQUEST['type_id'])? $_REQUEST['type_id']:'' ;
$shop_name = isset($_REQUEST['shop_name'])? $_REQUEST['shop_name']:'' ;

//$country = isset($_REQUEST['country'])? $_REQUEST['country']:0;
$province = isset($_REQUEST['province'])? $_REQUEST['province']:0;
$city = isset($_REQUEST['city'])? $_REQUEST['city']:0;
$district = isset($_REQUEST['district'])? $_REQUEST['district']:0;
$stree = isset($_REQUEST['stree'])? $_REQUEST['stree']:'';
//---------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------

$wheres = '';
if($type_id!= '')
{
    $wheres .= "and type_id = '$type_id'";   
}
$supplier_id = '';
$sql = "SELECT distinct supplier_id FROM " .$GLOBALS['ecs']->table('supplier_shop_config');
$arr = $GLOBALS['db']->GetAll($sql);
if($arr){
	$temp = array();
	foreach($arr as $v){
		$temp[] = $v['supplier_id'];
	}
	$supplier_id = implode(',',$temp);
	$where = " and supplier_id in($supplier_id) ";
}
//---------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------

if($province)
{
    $sql = "SELECT supplier_id FROM " .$GLOBALS['ecs']->table('supplier_shop_config') .
		    " WHERE code='shop_province' and value = ".$province;
		$arr = $GLOBALS['db']->GetAll($sql);

		if($arr){
			$temp = array();
			foreach($arr as $v){
				$temp[] = $v['supplier_id'];
			}
			$supplier_id = implode(',',$temp);
			$where = " and supplier_id in($supplier_id) ";
			if($city)
			{
			    $sql = "SELECT supplier_id FROM " .$GLOBALS['ecs']->table('supplier_shop_config') .
					    " WHERE code='shop_city' and value = ".$city." and supplier_id in ($supplier_id)";
					$arr = $GLOBALS['db']->GetAll($sql);
					if($arr){
						$temp = array();
						foreach($arr as $v){
							$temp[] = $v['supplier_id'];
						}
						$supplier_id = implode(',',$temp);
						$where = " and supplier_id in($supplier_id) ";
						if($district)
						{
						    $sql = "SELECT supplier_id FROM " .$GLOBALS['ecs']->table('supplier_shop_config') .
								    " WHERE code='shop_district' and value = ".$district." and supplier_id in ($supplier_id)";
								$arr = $GLOBALS['db']->GetAll($sql);
								if($arr){
									$temp = array();
									foreach($arr as $v){
										$temp[] = $v['supplier_id'];
									}
									$supplier_id = implode(',',$temp);
									$where = " and supplier_id in($supplier_id) ";
									if($stree!='')
									{
									    $sql = "SELECT supplier_id FROM " .$GLOBALS['ecs']->table('supplier_shop_config') .
											    " WHERE code='shop_address' and value like '%".$shop_name."%' and supplier_id in ($supplier_id)";
											$arr = $GLOBALS['db']->GetAll($sql);
											if($arr){
												$temp = array();
												foreach($arr as $v){
													$temp[] = $v['supplier_id'];
												}
												$supplier_id = implode(',',$temp);
												$where = " and supplier_id in($supplier_id) ";
											}else{
												$supplier_id = -1;
												$where = " and supplier_id = -1 ";
											}				
									}	
									
								}else{
									$supplier_id = -1;
									$where = " and supplier_id = -1 ";
								}				
						}	
					}else{
						$supplier_id = -1;
						$where = " and supplier_id = -1 ";
					}				
			}	
		}else{
			$supplier_id = -1;	
			$where = " and supplier_id = -1 ";	
		}	
		//echo $supplier_id;exit;	
}


//---------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------



if($shop_name != '' && $supplier_id!= -1)
{
		if($supplier_id!=''){
			$sql = "SELECT supplier_id FROM " .$GLOBALS['ecs']->table('supplier_shop_config') .
		    " WHERE code='shop_name' and value like '%".$shop_name."%' $where";
		}else{
			$sql = "SELECT supplier_id FROM " .$GLOBALS['ecs']->table('supplier_shop_config') .
		    " WHERE code='shop_name' and value like '%".$shop_name."%'";
		}
		
		$arr = $GLOBALS['db']->GetAll($sql);
		if($arr){
			$temp = array();
			foreach($arr as $v){
				$temp[] = $v['supplier_id'];
			}
			$supplier_id = implode(',',$temp);
			$where = " and supplier_id in(".$supplier_id.") ";
		}else{
			$where = " and supplier_id = -1";
		}	
}
// print_r($where);die;and supplier_id in(1,2,4,5,6,7,8,10,11,18,21,22,23)
//---------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------
//TODO:需要includes/pager.class.php-------------------------------------------------------------------
$wheres .= $where;	
$page = (isset($_REQUEST['page'])) ? intval($_REQUEST['page']) : 1;
$pagesize = 5;
$offset = ($page-1)*$pagesize;
$item = $GLOBALS['db']->getOne("SELECT COUNT(*) AS num FROM " .$GLOBALS['ecs']->table('supplier') .
    " WHERE status=1 $wheres order by rank_id desc,system_fee desc,type_id asc,add_time desc");
$totalPage = ceil($item['num']/$pagesize);  
$page_link = $_SERVER['PHP_SELF'].'?type_id='.$type_id.'&shop_name='.$shop_name.'&province='.$province.'&city='.$city.'&district='.$district.'&stree='.$stree;
require(dirname(__FILE__) . '/includes/pager.class.php');
$pager = new pager($totalPage,$page,5,$page_link); // $pager对象 
$pages = $pager->showpager(); // 输出分页 

$sql = "SELECT supplier_id,contacts_name,contacts_phone FROM " .$GLOBALS['ecs']->table('supplier') .
    " WHERE status=1 $wheres order by rank_id desc,system_fee desc,type_id asc,add_time desc limit $offset,$pagesize";
   
$maps_list = $GLOBALS['db']->GetAll($sql);
//---------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------

$lists = array();
$jishu = 1;
foreach($maps_list as $k => $v){
	$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('supplier_shop_config') .
	    " WHERE supplier_id = ".$v['supplier_id']." and code in('shop_name','shop_country','shop_province','shop_city','shop_district','shop_address','gdmaps') order by id";
	$temp = $GLOBALS['db']->GetAll($sql);
	$v['shop_name'] = $temp[0]['value']; 
	$sql = "SELECT region_name FROM " .$GLOBALS['ecs']->table('region') .
	    " WHERE region_id in('".$temp[1]['value']."','".$temp[2]['value']."','".$temp[3]['value']."','".$temp[4]['value']."') order by region_id";
	$region = $GLOBALS['db']->GetAll($sql);

	
	$v['country'] = $temp[1]['value']; 
	$v['province'] = $temp[2]['value']; 
	$v['city'] = $temp[3]['value']; 
	$v['district'] = $temp[4]['value']; 
	$v['address'] = $region[0]['region_name'].$region[1]['region_name'].$region[2]['region_name'].$region[3]['region_name'].$temp[5]['value']; 	
	$v['gdmaps'] = $temp[6]['value']; 
	$v['num']	= $jishu;
	$jishu++; 
	$lists[] = $v;
}

//---------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------



$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('street_category') .
    " WHERE is_show=1 ";
$street_category = $GLOBALS['db']->GetAll($sql);

$supplier_country = $_CFG['shop_country'];
$GLOBALS['smarty']->assign('country_list',       get_regions());	
$GLOBALS['smarty']->assign('province_list', get_regions(1, $supplier_country));
$GLOBALS['smarty']->assign('city_list', get_regions(2, $supplier['province']));
$GLOBALS['smarty']->assign('district_list', get_regions(3, $supplier['city']));
$GLOBALS['smarty']->assign('supplier_country', $supplier_country);

$smarty->assign('street_category',$street_category);
$smarty->assign('lists',$lists);
$smarty->assign('pages',$pages);
$smarty->assign('type_id',$type_id);
$smarty->assign('shopname',$shop_name);

$cache_id = sprintf('%X', crc32(date('ymd' . '-' . $filter)));

/* 模板赋值 */
assign_template();
$position = assign_ur_here(0, $GLOBALS["_LANG"]["gdmaps"]);
$smarty->assign('page_title',       $position['title']);       // 页面标题
$smarty->assign('ur_here',          $position['ur_here'] . '> ' . $topic['title']);     // 当前位置
$smarty->assign('helps',            get_shop_help()); // 网店帮助
$smarty->assign('all',   	$cats['all']);
$smarty->assign('logopath',		'/'.DATA_DIR.'/supplier/logo/');
/*header css选择 */
 $smarty->assign('header_style','68ecshop_common_package.css');
/* 显示模板 */

$smarty->display('gdmaps.dwt');





// 换成小京东的数据库就有标记点了。
?>